package com.keyin.android.util;

public class DataConvert
{
	public static int byte2int(byte[] b,int nStartIndex)
	{
		int i1 = b[nStartIndex] >= 0 ? b[nStartIndex] : 256+b[nStartIndex];
		int i2 = b[nStartIndex+1] >= 0 ? b[nStartIndex+1] : 256+b[nStartIndex+1];
		int i3 = b[nStartIndex+2] >= 0 ? b[nStartIndex+2] : 256+b[nStartIndex+2];
		int i4 = b[nStartIndex+3] >= 0 ? b[nStartIndex+3] : 256+b[nStartIndex+3];
		return ((i1 << 24) + (i2 << 16) + (i3 << 8) + (i4 << 0));
	}
	
	public static int byte2short(byte[] b,int nStartIndex)
	{
		int i1 = b[nStartIndex] >= 0 ? b[nStartIndex] : 256+b[nStartIndex];
		int i2 = b[nStartIndex + 1] >= 0 ? b[nStartIndex+1] : 256+b[nStartIndex+1];
		return ((i1 << 8) + (i2 << 0));
	}
	
	public static void writeInt(byte[] b,int nStartIndex,int aIntvalue)
	{
		b[3 + nStartIndex] = (byte)(aIntvalue & 0x00ff);
		b[2 + nStartIndex] = (byte)((aIntvalue >> 8) & 0x00ff);
		b[1 + nStartIndex] = (byte)((aIntvalue >> 16) & 0x00ff);
		b[0 + nStartIndex] = (byte)((aIntvalue >> 24) & 0x00ff);
	}
	
	public static void writeShort(byte[] b,int nStartIndex,short aShortValue)
	{
		b[1 + nStartIndex] = (byte)(aShortValue & 0x00ff);
		b[0 + nStartIndex] = (byte)((aShortValue >> 8) & 0x00ff);
	}
}
